package sg.nus.iss.vmcs.gui.elements;

import java.awt.Component;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionListener;
import sg.nus.iss.vmcs.utils.Constant;

/**
 * Labelled Display consisting of a description label and display textfield<br><br>
 * Modifications:
 * <li>27/12/2010 - Philip - Tidied up class and removed global imports</li>
 * <li>30/12/2010 - Philip - Introduced the Item Display Strategy to standardize item display</li>
 * <br>
 * <br>
 * @author Pang Pingli
 * @author Philip
 */
public class LabelledDisplay extends StoreViewerItem {

	/** Generated Serial ID */
	private static final long serialVersionUID = 3621322458825958281L;
	
	/** Default Length for the text field */
	public final static int DEFAULT = 5;
	
	/** Label describing the content */
	private Label lb;
	
	/** Textfield holding onto display content */
	private TextField value;

	/**
	 * Creates a new instance of the labelled display
	 * @param label Name of the Labelled display
	 * @param length Length of the TextField
	 * @param ids Display Strategy Required
	 */
	public LabelledDisplay(String label, int length, ItemDisplayStrategy ids) {
		super(ids);
		
		//Setting Up UI Elements
		lb = new Label();
		lb.setAlignment(Label.RIGHT);
		lb.setFont(Constant.LABEL_FONT);
		setName(label);
		
		value = new TextField();
		value.setColumns(length);
		
		ids.setupPanelLayout(this, lb, value);
		setVariableDisplayState(VariableDisplayState.getDisplayState(VariableDisplayState.Type.Editable));
		setDisplay();
	}

	/* 
	 * Returns textfield to be set
	 */
	@Override
	public Component getDisplayComponent() {
		return value;
	}

	/* 
	 * Sets the variable display state
	 */
	@Override
	public void setDisplay() {
		vds.setDisplayComponent(this);
	}

	@Override
	public void setName(String name) {
		lb.setText(name);		
	}
	
	/**
	 * If labelled display used for password input, set this to hide display
	 * @param c
	 */
	public void setEchoChar(char c){
		value.setEchoChar(c);
	}

	/**
	 * Sets the Value textfield with the color and input value
	 * @param vl Value to be set
	 */
	@Override
	public void setQuantity(int qty) {
		setValue(qty);
	}

	/**
	 * Sets the Value textfield with the color and input value
	 * @param vl Value to be set
	 */
	@Override
	public void setValue(String value) {
		this.value.setText(value);
	}

	@Override
	public void addListener(ActionListener listener) {
		value.addActionListener(listener);
	}

	/* (non-Javadoc)
	 * @see sg.nus.iss.vmcs.gui.elements.StoreViewerItem#setActive(boolean)
	 */
	@Override
	public void setActive(boolean active) {
		value.setEnabled(active);
		lb.setEnabled(active);
		value.setEditable(active);
	}
	
	/**
	 * Sets whether the value textbox is editable
	 * @param editable True to set editable, false if otherwise
	 * 
	 */
	public void setEditable(boolean editable){
		if(! editable) {
			setVariableDisplayState(VariableDisplayState.getDisplayState(VariableDisplayState.Type.UnEditable));
			setDisplay();
		}
		
		value.setEditable(editable);
	}

	/* (non-Javadoc)
	 * @see sg.nus.iss.vmcs.models.StoreItemObserver#notifyChange(java.lang.String, int, int)
	 */
	@Override
	public void notifyChange(String name, int qty, int value) {
		this.setName(name);
		this.setValue(qty);
	}
	
	@Override
	public void setActionCommand(String command) {
		//Not Implemented / Required
	}
}